//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Business252.h"
using namespace Cephei::QL::Times::Daycounters;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Times::Daycounters::CBusiness252::CBusiness252 (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::ICalendar^>^ c) : CDayCounter(CBusiness252::typeid)
{
    CCalendar^ _Cc;
    try
    {
#ifdef HANDLE
        _phBusiness252 = NULL;
#endif
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::ICalendar^>::IsSome::get (c))
        {
            _Cc = safe_cast<CCalendar^> (c->Value);
            _Cc->Lock();
        }
        QuantLib::Calendar _c = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::ICalendar^>::IsSome::get (c) ? static_cast<QuantLib::Calendar> (_Cc->GetReference ()) : QuantLib::Brazil()); //1
        _ppBusiness252 = new boost::shared_ptr<QuantLib::Business252> (new QuantLib::Business252 ( _c ));
        SetDayCounter (boost::dynamic_pointer_cast<QuantLib::DayCounter> (*_ppBusiness252));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc != nullptr) _Cc->Unlock();
    }
}
Cephei::QL::Times::Daycounters::CBusiness252::CBusiness252 (boost::shared_ptr<QuantLib::Business252>& childNative, Object^ owner) : CDayCounter(CBusiness252::typeid)
{
#ifdef HANDLE
	_phBusiness252 = NULL;
#endif
	_ppBusiness252 = &childNative;
    _ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (boost::dynamic_pointer_cast<QuantLib::DayCounter> (*_ppBusiness252));
}
Cephei::QL::Times::Daycounters::CBusiness252::CBusiness252 (QuantLib::Business252& childNative, Object^ owner) : CDayCounter(CBusiness252::typeid)
{
#ifdef HANDLE
	_phBusiness252 = NULL;
#endif
	_ppBusiness252 = new boost::shared_ptr<QuantLib::Business252> (&childNative);
    _ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (boost::dynamic_pointer_cast<QuantLib::DayCounter> (*_ppBusiness252));
    _Business252Owner = owner;
    _DayCounterOwner = owner;
}

Cephei::QL::Times::Daycounters::CBusiness252::CBusiness252 (CBusiness252^ copy) : CDayCounter(CBusiness252::typeid)
{
#ifdef HANDLE
	_phBusiness252 = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBusiness252 = new boost::shared_ptr<QuantLib::Business252> (copy->GetShared());
        _ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (boost::dynamic_pointer_cast<QuantLib::DayCounter> (*_ppBusiness252));
    }
}
Cephei::QL::Times::Daycounters::CBusiness252::CBusiness252 (System::Type^ t) : CDayCounter(CBusiness252::typeid)
{
#ifdef HANDLE
	_phBusiness252 = NULL;
#endif
	if (!t->IsSubclassOf(CBusiness252::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::Daycounters::CBusiness252::CBusiness252 (QuantLib::Handle<QuantLib::Business252>& childNative, Object^ owner)  : CDayCounter(CBusiness252::typeid)
{
	_phBusiness252 = &childNative;
	_ppBusiness252 = &static_cast<boost::shared_ptr<QuantLib::Business252>>(childNative.currentLink());
    _ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (boost::dynamic_pointer_cast<QuantLib::DayCounter> (*_ppBusiness252));
    _Business252Owner = owner;
}
Cephei::QL::Times::Daycounters::CBusiness252::CBusiness252 (QuantLib::Handle<QuantLib::Business252> childNative)  : CDayCounter(CBusiness252::typeid)
{
	_phBusiness252 = &childNative;
	_ppBusiness252 = &static_cast<boost::shared_ptr<QuantLib::Business252>>(childNative.currentLink());
    _ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (boost::dynamic_pointer_cast<QuantLib::DayCounter> (*_ppBusiness252));
}
#endif
#ifdef STRUCT
Cephei::QL::Times::Daycounters::CBusiness252::CBusiness252 (QuantLib::Business252 childNative)  : CDayCounter(CBusiness252::typeid)
{
#ifdef HANDLE
	_phBusiness252 = NULL;
#endif
	_ppBusiness252 = new boost::shared_ptr<QuantLib::Business252> (new QuantLib::Business252 (childNative));
    _ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (boost::dynamic_pointer_cast<QuantLib::DayCounter> (*_ppBusiness252));
}
#endif

Cephei::QL::Times::Daycounters::CBusiness252::~CBusiness252 ()
{
    if (_ppBusiness252 != NULL)
    {
	    delete _ppBusiness252;
        _ppBusiness252 = NULL;
    }
}
Cephei::QL::Times::Daycounters::CBusiness252::!CBusiness252 ()
{
    if (_ppBusiness252 != NULL)
    {
	    delete _ppBusiness252;
    }
}
QuantLib::Business252& Cephei::QL::Times::Daycounters::CBusiness252::GetReference ()
{
    if (_ppBusiness252 == NULL) throw gcnew NativeNullException ();
	return **_ppBusiness252;
}
boost::shared_ptr<QuantLib::Business252>& Cephei::QL::Times::Daycounters::CBusiness252::GetShared ()
{
    if (_ppBusiness252 == NULL) throw gcnew NativeNullException ();
	return *_ppBusiness252;
}
QuantLib::Business252* Cephei::QL::Times::Daycounters::CBusiness252::GetPointer ()
{
    if (_ppBusiness252 == NULL) throw gcnew NativeNullException ();
	return &**_ppBusiness252;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Business252>& Cephei::QL::Times::Daycounters::CBusiness252::GetHandle ()
{
	if (_phBusiness252 == NULL)
	{
		_phBusiness252 = new Handle<QuantLib::Business252> (*_ppBusiness252);
	}
	return *_phBusiness252;
}
#endif
bool Cephei::QL::Times::Daycounters::CBusiness252::HasNative () 
{
	return (_ppBusiness252 != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::Daycounters::IBusiness252^ Cephei::QL::Times::Daycounters::CBusiness252_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::ICalendar^>^ c)
{
    return gcnew CBusiness252 ( c);
}
